﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoolERP_Entities;

namespace CoolERP_DAL
{
    public interface IKCDAL : IDAL<KC>
    {
        List<WLKCModel> GetWLKC(Func<WLKCModel, bool> exp, bool isDistinct, string orderExpr, int? pageIndex, int pageSize, out int totalCount);
        List<BZXKCModel> GetBZXKC(Func<BZXKCModel, bool> exp, bool isDistinct, string orderExpr, int? pageIndex, int pageSize, out int totalCount);
        List<KCMXModel> GetKCMX(Func<KCMXModel, bool> exp, string orderExpr, int? pageIndex, int pageSize, out int totalCount);
    }

    public class KCDAL : BaseDAL<KC>, IKCDAL
    {
        public KCDAL(IUnitOfWork context)
            : base(context)
        {
        }

        /// <summary>
        /// 物料库存
        /// </summary>
        /// <param name="isDistinct">是否执行唯一过滤</param>
        public List<WLKCModel> GetWLKC(Func<WLKCModel, bool> exp, bool isDistinct, string orderExpr, int? pageIndex, int pageSize, out int totalCount)
        {
            var result = from k in dataContext.KC.Where(t => t.WPLX == "01")
                         join w in dataContext.ZL_WL on k.WPID equals w.ID
                         join l in dataContext.CS_WLLX on w.LXBH equals l.BH
                         join d in dataContext.CS_JBDW on k.DW equals d.BH
                         join cw in dataContext.ZL_CW on k.CWID equals cw.ID
                         join ck in dataContext.ZL_CK on cw.CKID equals ck.ID
                         select new WLKCModel
                         {
                             ID = k.ID,
                             BH = w.BH,
                             WPID = k.WPID,
                             WPLX = k.WPLX,
                             MC = w.MC,
                             LXBH = w.LXBH,
                             LXMC = l.MC,
                             GG = w.GG,
                             YS = dataContext.CS_YS.FirstOrDefault(t => t.BH == w.YSBH && t.YSLXBH == w.YSLXBH).MC,
                             ZDKCSL = w.ZDKCSL,
                             GYSID = w.GYSID,
                             GYSMC = dataContext.ZL_GYS.FirstOrDefault(t => t.ID == w.GYSID).MC,
                             GYSJC = dataContext.ZL_GYS.FirstOrDefault(t => t.ID == w.GYSID).JIANCHENG,
                             KCZL = dataContext.KC.Where(t => t.WPID == k.WPID).Sum(x => x.SL),
                             PJDJ = k.PJDJ,
                             ZXDJ = k.ZXDJ,
                             DWBH = k.DW,
                             DWMC = d.MC,
                             JLDWMC = dataContext.CS_JBDW.Where(t => t.BH == w.JLDWBH).Count() > 0 ? dataContext.CS_JBDW.FirstOrDefault(t => t.BH == w.JLDWBH).MC : "",
                             SL = k.SL,
                             CKID = k.CKID,
                             CKMC = ck.MC,
                             CWID = cw.ID,
                             CWMC = cw.QU + "-" + cw.HANG + "-" + cw.LIE + "-" + cw.CENG + "-" + cw.GE,
                             QU = cw.QU,
                             HANG = cw.HANG,
                             LIE = cw.LIE,
                             CENG = cw.CENG,
                             GE = cw.GE
                         };
            result = result.Where(exp).AsQueryable();
            if (isDistinct)
                result = result.Distinct(Equality<WLKCModel>.CreateComparer(t => t.WPID));
            totalCount = result.Count();
            if (!string.IsNullOrEmpty(orderExpr))
                result = SortingExtensions.OrderUsingSortExpression(result, orderExpr);
            if (pageIndex == null)
                return result.ToList();
            else
                return result.Skip(Convert.ToInt32(--pageIndex) * pageSize).Take(pageSize).ToList();
        }

        /// <summary>
        /// 包装箱库存
        /// </summary>
        /// <param name="isDistinct">是否执行唯一过滤</param>
        public List<BZXKCModel> GetBZXKC(Func<BZXKCModel, bool> exp, bool isDistinct, string orderExpr, int? pageIndex, int pageSize, out int totalCount)
        {
            var result = from k in dataContext.KC
                         join b in dataContext.ZL_CPBZX on k.WPID equals b.ID
                         join cw in dataContext.ZL_CW on k.CWID equals cw.ID
                         join ck in dataContext.ZL_CK on cw.CKID equals ck.ID
                         select new BZXKCModel
                         {
                             ID = k.ID,
                             BH = b.BH,
                             WPLX = k.WPLX,
                             WPID = k.WPID,
                             MC = b.MC,
                             LX = b.BZXLX,
                             KCZL = dataContext.KC.Where(t => t.WPID == k.WPID).Sum(x => x.SL),
                             BZFS = b.BZFS,
                             WChang = b.WCHANG,
                             WKuan = b.WKUAN,
                             WGao = b.WGAO,
                             TJ = b.TJ,
                             ZL = b.ZL,
                             BZ = b.BZ,
                             CPXBH = b.CPXBH,
                             CPHXCBH = b.CPMCBH,
                             SL = k.SL,
                             CKID = k.CKID,
                             CKMC = ck.MC,
                             CWID = cw.ID,
                             CWMC = cw.QU + "-" + cw.HANG + "-" + cw.LIE + "-" + cw.CENG + "-" + cw.GE,
                             QU = cw.QU,
                             HANG = cw.HANG,
                             LIE = cw.LIE,
                             CENG = cw.CENG,
                             GE = cw.GE  
                         };
            result = result.Where(exp).AsQueryable();
            if (isDistinct)
                result = result.Distinct(Equality<BZXKCModel>.CreateComparer(t => t.WPID));
            totalCount = result.Count();
            if (!string.IsNullOrEmpty(orderExpr))
                result = SortingExtensions.OrderUsingSortExpression(result, orderExpr);
            if (pageIndex == null)
                return result.ToList();
            else
                return result.Skip(Convert.ToInt32(--pageIndex) * pageSize).Take(pageSize).ToList();
        }

        /// <summary>
        /// 库存明细
        /// </summary>
        public List<KCMXModel> GetKCMX(Func<KCMXModel, bool> exp, string orderExpr, int? pageIndex, int pageSize, out int totalCount)
        {
            var result = from k in dataContext.KC
                         join cw in dataContext.ZL_CW on k.CWID equals cw.ID
                         join ck in dataContext.ZL_CK on cw.CKID equals ck.ID
                         select new KCMXModel
                         {
                             ID = k.ID,
                             WPID = k.WPID,
                             SL = k.SL,
                             ZXDJ = k.ZXDJ,
                             PJDJ = k.PJDJ,
                             CWID = k.CWID,
                             CKID = k.CKID,
                             CKMC = ck.MC,
                             QU = cw.QU,
                             HANG = cw.HANG,
                             LIE = cw.LIE,
                             CENG = cw.CENG,
                             GE = cw.GE
                         };
            result = result.Where(exp).AsQueryable();
            totalCount = result.Count();
            if (!string.IsNullOrEmpty(orderExpr))
                result = SortingExtensions.OrderUsingSortExpression(result, orderExpr);
            if (pageIndex == null)
                return result.ToList();
            else
                return result.Skip(Convert.ToInt32(--pageIndex) * pageSize).Take(pageSize).ToList();
        }
    }
}
